package threadTest.example;

import createModel.prototypeModel.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class SyncUser {


	public static void main(String[] args) {
		System.out.println("同步用户定时任务");
		int MAX_SIZE = 1000;

		List<User> userList = new ArrayList<>(MAX_SIZE);
		for (int index = 0; index < MAX_SIZE; index++) {
			User user = new User();
			userList.add(user);
			user.setUserName("张三"+index);
			user.setPassword("张三"+index);
			user.setBirthday(new Date());
		}
		// 对数据进行分组
		List<List<User>> userPartionList  = new ArrayList<>();
		int end = userList.size();
		int begin = 0;
		int size = 10;
		for (int index = 0; index < userList.size() && begin < userList.size(); index++) {
			userPartionList.add(userList.subList(begin, Math.min(end, begin + size)));
			begin = index == 0 ? begin++ : begin;
			begin += size;
		}

		UserContatiner userContatiner = new UserContatiner(100);
		new Thread(()->{
			for (List<User> users : userPartionList) {
				try {
					UserProducer userProducer = new UserProducer(userContatiner);
					userProducer.setUsers(users);
					userProducer.produce();
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
			}
		},"用户生产者").start();

		new Thread(()->{
			for (List<User> users : userPartionList) {
				try {
					new UserComsumer(userContatiner).comsumer();
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
			}
		},"消费者").start();
	}
}
